package com.fairytask.ui.item.chat
{
	import com.fairytask.engine.define.Filter;
	import com.fairytask.event.ChatEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ChatList extends Sprite
	{
		private var btnFilter:Array	=	null;
		private var btnGray:Array		=	null;
		//容器
		private var _itemContainer		:Sprite;
		
		//皮肤
		private var _itemBtnSkin 		:String =		"";
		private var _itemCheckBoxSkin	:String =		"";
		
		
		//设定
		private var _left			:Number	=	0;
		private var _top			:Number	=	0;
		private var _itemSpace		:Number	=	0;
		
		private var _fixedBtnWidth	:Number	=	0;
		private var _fixedBtnHeight	:Number	=	0;
		
		//数据
		private var  _listData		:Array	=	[];
		private var  _checkBoxData	:Array	=	[];
		private var  _btnHeight	:int;
	
		public function ChatList(w:int,h:int,btnHeight:int,itemData:Array,itemBtnSkinStr:String="",itemCheckBoxSkinStr:String="")
		{
			super();
			
			//实例化置灰工具
			btnFilter			=	[Filter.BUTTON_DISABLED_FILTER];
			btnGray=[Filter.GRAY];
			
			_btnHeight			=	btnHeight;
			
			_fixedBtnWidth		=	w;
			_fixedBtnHeight		=	h;
			initGUI();
			
			_itemBtnSkin		=	itemBtnSkinStr;
			_itemCheckBoxSkin 	= 	itemCheckBoxSkinStr;
			
			listData			=	itemData;
			
		}
		
		private function initGUI():void
		{
			//初始化容器
			_itemContainer	=	new Sprite();
			addChild(_itemContainer);
			
		}
		
		public function get	checkBoxData():Array
		{
			return _checkBoxData;
		}
		
		private function formatList():void
		{
			var tmpHeight:Number	=	top;
			for(var i:int=0;i<_itemContainer.numChildren;i++)
			{
				var tmpItem:ChatListItem	=	_itemContainer.getChildAt(i) as ChatListItem;
				tmpItem.id	=	i;
				tmpItem.x	=	left;
				tmpItem.y	=	tmpHeight;
				tmpHeight	=	tmpHeight	+	tmpItem.height +itemSpace;
				tmpItem.itemBtn.addEventListener(MouseEvent.CLICK,onItemClick);
				/*if((!tmpItem.itemBtn.hasEventListener(MouseEvent.CLICK)))//临时屏蔽帮派点击事件
				{
					tmpItem.itemBtn.addEventListener(MouseEvent.CLICK,onItemClick);
				}*/
			}
			
		}
		
		private function onItemClick(e:MouseEvent):void
		{
			dispatchEvent(new ChatEvent(ChatEvent.CHAT_LIST_BTN_CLICK,null));
		}
		
		private function clearItem():void
		{
			while(_itemContainer.numChildren>0)
			{
				_itemContainer.removeChildAt(0);
			}
		}
		
		/**
		 * 
		 * @param index
		 * @return 
		 */
		public function getItem(index:uint):ChatListItem
		{
			var tmpItem:ChatListItem;
			if(index<_itemContainer.numChildren)
			{
				tmpItem	=	_itemContainer.getChildAt(index) as ChatListItem;
			}
			return tmpItem;
		}
		/**
		 * 
		 * 
		 * 删除 
		 * 
		 */		
		public function removeItem(item:ChatListItem):void
		{
			_itemContainer.removeChild(item);
			
			while(_itemContainer.numChildren>0)
			{
				
			}
		}
		/**
		 * 
		 * @param func
		 */
		public function getItemForEach(func:Function):void
		{
			for(var i:uint = 0 ; i < _itemContainer.numChildren ; i ++)
			{
				func.call(this,_itemContainer.getChildAt(i));
			}
		}
		/**
		 * 
		 * @return 
		 */
		public function get listData():Array
		{
			return _listData;
		}
		
		/**
		 * 传入以:data格式:{label:"测试0"} 的数组
		 * @param value
		 */
		public function set listData(value:Array):void
		{
			clearItem();
			if(value==null)
			{
				return;
			}
			_listData = value;
			
			for(var i:int=0;i<_listData.length;i++)
			{
				var tmpObj	:Object		=	_listData[i];
				var tmpItem:ChatListItem 	= 	new ChatListItem(tmpObj["label"],_fixedBtnWidth-left*2,_btnHeight,i,_itemBtnSkin,_itemCheckBoxSkin);
				//临时屏蔽帮派频道
				/*if(i==4)
				{
					tmpItem.filters		=		btnGray;
					tmpItem.itemCheckBox.canUse	=	false;
				}*/
				_checkBoxData.push(tmpItem.itemCheckBox);
				_itemContainer.addChild(tmpItem);
				
			}
			formatList();
		}
		
		
		/**
		 * 
		 * @return 
		 */
		public function get top():Number
		{
			return _top;
		}
		
		/**
		 * 
		 * @param value
		 */
		public function set top(value:Number):void
		{
			_top = value;
		}
		
		/**
		 * 
		 * @return 
		 */
		public function get left():Number
		{
			return _left;
		}
		
		/**
		 * 
		 * @param value
		 */
		public function set left(value:Number):void
		{
			_left = value;
			formatList();
		}
		
		/**
		 * 
		 * @return 
		 */
		public function get itemSpace():Number
		{
			return _itemSpace;
		}
		
		/**
		 * 
		 * @param value
		 */
		public function set itemSpace(value:Number):void
		{
			_itemSpace = value;
			formatList();
		}
		

	}
}